/**
 * 
 */
package com.shy.portal.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.shy.base.controller.AbsController;
import com.shy.base.util.BeanUtil;
import com.shy.base.util.IpUtil;
import com.shy.base.vo.AppConstants;
import com.shy.base.vo.EasyuiPagerVo;
import com.shy.base.vo.PagerFilter;
import com.shy.portal.entity.SysConfig;
import com.shy.portal.service.SysConfigService;

/**
 * <p>Title:SysConfigController </p>
 * <p>Description: 系统参数 controller层</p>
 * <p>Company:shy </p> 
 * @author H2013788
 * @date 2019年09月10日
 */
@Controller
@RequestMapping("sysConfig")
public class SysConfigController extends AbsController {
	
	/**
     * log
     */
	private Logger logger = LogManager.getLogger(this.getClass());

	/**
	 * service层
	 */
    @Resource
    private SysConfigService sysConfigService;

	/**
	 * 加载页面
	 * @return
	 * @author H2013788
	 * @date 2019年09月10日
	 */
	@RequestMapping("page")
	public String loadPage() {
		return "portal/sys_config_list";
	}
	
	/**
	 * 加载数据
	 * 
	 * @author H2013788
	 * @date 2019年09月10日
	 */
	@RequestMapping("data")
	public void loadData(SysConfig sysConfig, Integer page, Integer rows, HttpServletResponse response) {
		PagerFilter pagerFilter = getPageable(rows == null ? 15L : rows);
        pagerFilter.setPagePer(null == page ? 1 : page);
        
        String[] likes = { "propertyName", "propertyValue" }; 
        Map<String, String[]> filterMap = new HashMap<>();
        filterMap.put(AppConstants.LIKE_TYPES, likes);
        
        pagerFilter = sysConfigService.listByPager(sysConfig, filterMap, pagerFilter);
        //输出json
        writeResponse(new StringBuffer(gsonDateTime.toJson(new EasyuiPagerVo(pagerFilter))), response);
	}
	
	/**
	 * 编辑
	 * 
	 * @author H2013788
	 * @date 2019年09月10日
	 */
	@RequestMapping("edit")
	public void eidt(Integer id, HttpServletResponse response) {
		if (null != id) {
			SysConfig sysConfig = sysConfigService.getById(id);
            //输出json
            writeResponse(new StringBuffer(gsonDateTime.toJson(sysConfig)), response);
        } else {
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.GET_OBJECT_NULL), response);
        }
	} 
	
	
	/**
	 * 保存
	 * 
	 * @author H2013788
	 * @date 2019年09月10日
	 */
	@RequestMapping("save")
	public void save(SysConfig sysConfig, HttpServletResponse response) {
		if (null == sysConfig) {
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.REQUEST_PARAM_NULL), response);
        } else {
            try {
                if (null != sysConfig.getId()) {
                	//编辑
                	sysConfig.setUpdateTime(new Date());
                	sysConfig.setUpdateUser(getShiroUser().getLoginName());
                	SysConfig old = sysConfigService.getById(sysConfig.getId());
                    BeanUtil.merge(old, sysConfig);
                    sysConfigService.update(old);
                    AppConstants.configMap.put(old.getPropertyName(), old.getPropertyValue());
                    respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.SAVE_SUCCESS), response);
                } else {
					sysConfig.setCreateUser(getShiroUser().getLoginName());
					sysConfig.setCreateTime(new Date());
					sysConfigService.save(sysConfig);
					AppConstants.configMap.put(sysConfig.getPropertyName(), sysConfig.getPropertyValue());
					respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.SAVE_SUCCESS), response);
                }
            } catch (Exception e) {
                e.printStackTrace();
                respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.SAVE_ERROR), response);
                logger.error(AppConstants.configMap.get(AppConstants.SAVE_ERROR) + e);
            }
        }
	}
	
	/**
	 * 刪除数据
	 * 
	 * @author H2013788
	 * @date 2019年09月10日
	 */
	//系统配置不允許刪除
	//@RequestMapping("delete")
	public void delete(Integer id, HttpServletResponse response) {
		try {
            if (null != id) {
            	sysConfigService.deleteById(id);
                respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.OPERATE_SUCCESS), response);
            } else {
                respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.REQUEST_PARAM_NULL), response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.OPERATE_ERROR), response);
            logger.error(AppConstants.configMap.get(AppConstants.DELETE_ERROR) + e);
        }
	}
	
	
	/**
	 * 加载数据
	 * 
	 * @author H2013788
	 * @date 2019年09月10日
	 */
	@RequestMapping("ip")
	public void getIp(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> map = new HashMap<String, String>();
        //输出json
        try {
        	map.put("ip", IpUtil.getIpAddr(request));
        	map.put("localIp", IpUtil.getLocalIP());
			writeResponse(new StringBuffer(gsonDateTime.toJson(map)), response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("param")
	public void getParameter(HttpServletResponse response) {
		Gson gson = new Gson();
		writeResponse(new StringBuffer(gsonDateTime.toJson(AppConstants.configMap)), response);
	}
}
